package com.demo.user.util.page;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.demo.user.common.Constant;

import java.io.Serializable;
import java.util.List;

@SuppressWarnings("serial")
public class DataTablesRequestDto implements Serializable {

    private int draw;
    private int start;
    private int length;
    private List<DataTablesRequestColumnDto> columns;
    private List<DataTablesRequestOrderDto> order;
    private DataTablesRequestSearchDto search = new DataTablesRequestSearchDto();

    public int getDraw() {
        return draw;
    }

    public void setDraw(int draw) {
        this.draw = draw;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getLength() {
        return length <= 0 ? Constant.PAGE_DEFAULT_SIZE : length;
    }

    @JsonIgnore
    public int getOriginLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public List<DataTablesRequestColumnDto> getColumns() {
        return columns;
    }

    public void setColumns(List<DataTablesRequestColumnDto> columns) {
        this.columns = columns;
    }

    public List<DataTablesRequestOrderDto> getOrder() {
        return order;
    }

    public void setOrder(List<DataTablesRequestOrderDto> order) {
        this.order = order;
    }

    public DataTablesRequestSearchDto getSearch() {
        return search;
    }

    public void setSearch(DataTablesRequestSearchDto search) {
        this.search = search;
    }

    public PageParam toPageParam() {
        PageParam pageParam = constructPageParam();
        return pageParam;
    }

    public PageParam constructPageParam() {
        PageParam pageParam = new PageParam();
        pageParam.setPageNo(getStart() / getLength() + 1);
        pageParam.setPageSize(getLength());
        pageParam.setPageStart(getStart());

        return pageParam;
    }
}
